from .api import ApiClient


class SMSApiClient(ApiClient):
    def __init__(self, login: str, password: str):
        super().__init__(base_url="https://sms.atotx.su/")
        self.login = login
        self.password = password

    async def login_to_account(self):
        """
        Входим в аккаунт смс сервиса
        @param login: Логин
        @param password: Пароль

        POST https://sms.atotx.su/login/
        """

        data = {
            "username": self.login,
            "password": self.password,
        }

        response = await self.send_request("POST", "login/", data)
        if response:
            return response.get("token")

    async def send_sms_with_otp_code(
        self,
        otp_code: int,
        phone: str,
    ):
        """
        Отправляем код на номер телефона
        @param otp_code: Otp код
        @param phone: Номер телефона для отправки

        POST https://sms.atotx.su/sendcode/
        """

        access_token = await self.login_to_account()
        if not access_token:
            return False

        data = {
            "code": otp_code,
            "phone": phone,
        }
        self.session.headers.update({"Authorization": f"Bearer {access_token}"})
        return await self.send_request("POST", "sendcode/", data)
